#pragma once
#include <grpcpp/grpcpp.h>
#include "message.grpc.pb.h"
#include "const.h"
#include "Singleton.h"

using grpc::Channel;
using grpc::Status;
using grpc::ClientContext;
using message::GetVarifyReq;
using message::GetVarifyRsp;
using message::VarifyService;

class GRPConPool {
public:
	GRPConPool(size_t poolsize, std::string host, std::string port);
	~GRPConPool();

	void Close();

	std::unique_ptr<VarifyService::Stub> getConnection();
	void returnConnection(std::unique_ptr<VarifyService::Stub> context);
	


private:
	std::atomic<bool> _b_stop;
	size_t _pool_size;
	std::string _host;
	std::string _port;
	std::queue<std::unique_ptr<VarifyService::Stub>> _connection;
	std::mutex _mutex;
	std::condition_variable _cond;


};

class VerifyGrpcClient: public Singleton<VerifyGrpcClient>
{
	friend class Singleton < VerifyGrpcClient>;

public:
	GetVarifyRsp GetVerifyCode(std::string email);
private:
	VerifyGrpcClient();

	std::unique_ptr<GRPConPool> _pool;
};

